package shujia.day14;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;



/*
    登录注册第一个版本，用户的信息，使用两个字符串表示
 */

public class LoginDemo1 {
    private static final Map<String, String> ALL_USERS = getAllUsers();

    public static void main(String[] args) {
        init();
    }

    public static void init(){
        System.out.println("=================== 欢迎登录张成阳婚姻介绍所 ====================");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要做的操作：(1.登录 2.注册)");
        if(sc.hasNextInt()){
            switch (sc.nextInt()){
                case 1:
                    System.out.println();
                    System.out.println();
                    login();
                    break;
                case 2:
                    System.out.println();
                    System.out.println();
                    register();
                    break;
                default:
                    System.out.println();
                    System.out.println();
                    System.out.println("没有该选项！");
                    break;
            }
        }else {
            System.out.println("您输入的类型有误！");
        }

    }

    public static void login(){
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎登录！");
        System.out.println("请输入您的用户名：");
        String name = sc.next();
        // 从map集合中判断用户是否存在
        if(ALL_USERS.containsKey(name)){
            System.out.println("请输入用户的密码：");
            String pwd = sc.next();
            if(pwd.equals(ALL_USERS.get(name))){
                System.out.println("登录成功！！");
            }else {
                System.out.println("登录失败！密码不正确！");
            }
        }else {
            System.out.println("该用户还未注册！");
        }
    }

    public static void register(){
        BufferedWriter bw = null;
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("欢迎注册！");
            System.out.println("请设置您的用户名：");
            String name = sc.next();
            // 从map集合中判断用户是否存在
            if(ALL_USERS.containsKey(name)){
                System.out.println("该用户名已被使用！！");
            }else {
                System.out.println("请设置您的账户密码：");
                String pwd = sc.next();
                //将用户名和密码写入到文件中【追加写】
                bw = new BufferedWriter(new FileWriter("src/shujia/day14/users.txt", true));
                bw.write(name+"|"+pwd);
                bw.newLine();
                bw.flush();

                System.out.println("用户注册成功！！");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static Map<String,String> getAllUsers(){
        BufferedReader br = null;
        HashMap<String,String> userMap = new HashMap<>();
        try {
            //读取用户文件，将所有用户封装到Map集合中
            br = new BufferedReader(new FileReader("src/shujia/day14/users.txt"));
            String line = null;
            while ((line=br.readLine())!=null){
                String[] infoArr = line.split("\\|");
                String name = infoArr[0];
                String password = infoArr[1];
                userMap.put(name,password);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return userMap;

    }


}
